<#include "../include/header.html">
<script src="${base.contextPath}/common/profile?orderSubmitCtl=HAP_OM_ORDER_SUBMIT_CTL&orderApproveCtl=HAP_ OM_ORDER_APPROVE_CTL" type="text/javascript"></script>
<script type="text/javascript">
    var headerId = '${RequestParameters.headerId!0}';
    var queryFlag = '${RequestParameters.queryFlag!0}';
    var viewModel = Hap.createGridViewModel("#grid",{
        model: {},
        saveFunction: function () {
            var b = $("#myForm").data("kendoValidator").validate();
            if (!b) {
                Hap.showToast({
                    type: "info",
                    message: $l('<@spring.message "msg.error.coderulesline.filed_value_validation"/>')
                });
                return;
            }


            Hap.submitForm({
                url: '${base.contextPath}/hap/om/order/headers/submit',
                formModel: viewModel.model,
                grid: {"orderLinesList": $("#grid")},
                success: function (data) {
                    if (data.success) {
                        if (!headerId || headerId == 0) {
                            headerId = data.rows[0].headerId;
                        }
                        refreshForm();
                    }
                },
                failure: function (arg) {

                },
            });
        },


    });

    //form表单校验器
    window.onload = function () {
        var validator = $("#myForm").kendoValidator({
            messages: {
                required: '<@spring.message "msg.error.coderulesline.filed_value_validation"/>',
            },
            rules: {}
        }).data("kendoValidator");

        if(queryFlag == 1){
            $("#grid").data("kendoGrid").setOptions({
                editable: true
            });
            $("#grid_other").data("kendoGrid").setOptions({
                editable: true
            });

            document.getElementById("grid_new").style.display="";
            document.getElementById("grid_save").style.display="";
            document.getElementById("grid_remove").style.display="";
            formDivNoDisable();

        }else if (queryFlag == 2) {
            $("#grid").data("kendoGrid").setOptions({
                editable: false
            });
            $("#grid_other").data("kendoGrid").setOptions({
                editable: false
            });

            document.getElementById("delete").style.display="none";
            document.getElementById("save").style.display="none";
            document.getElementById("submit").style.display="none";

            document.getElementById("grid_new").style.display="none";
            document.getElementById("grid_save").style.display="none";
            document.getElementById("grid_remove").style.display="none";

            document.getElementById("approval").style.display="none";
            document.getElementById("reject").style.display="none";

            formDivDisable();

        };

        if ((viewModel.model.orderStatus=="NEW"||viewModel.model.orderStatus=="新建")&&queryFlag == 1) {
            document.getElementById("delete").style.display="";
            document.getElementById("save").style.display="";
            document.getElementById("submit").style.display="";

            document.getElementById("grid_new").style.display="";
            document.getElementById("grid_save").style.display="";
            document.getElementById("grid_remove").style.display="";

            document.getElementById("approval").style.display="none";
            document.getElementById("reject").style.display="none";
            formDivNoDisable();
        }else if ((viewModel.model.orderStatus=="SUBMITED"||viewModel.model.orderStatus=="已提交")&&queryFlag == 1) {
            document.getElementById("approval").style.display="";
            document.getElementById("reject").style.display="";

            document.getElementById("delete").style.display="none";
            document.getElementById("save").style.display="none";
            document.getElementById("submit").style.display="none";

            document.getElementById("grid_new").style.display="none";
            document.getElementById("grid_save").style.display="none";
            document.getElementById("grid_remove").style.display="none";

            $("#grid").data("kendoGrid").setOptions({
                editable: false
            });
            $("#grid_other").data("kendoGrid").setOptions({
                editable: false
            });
            formDivDisable();

        }else if( (viewModel.model.orderStatus=="APPROVED"||viewModel.model.orderStatus=="已审批")&&queryFlag == 1){

            document.getElementById("save").style.display="none";
            document.getElementById("submit").style.display="none";
            document.getElementById("approval").style.display="none";
            document.getElementById("reject").style.display="none";
            document.getElementById("delete").style.display="none";

            document.getElementById("grid_new").style.display="none";
            document.getElementById("grid_save").style.display="none";
            document.getElementById("grid_remove").style.display="none";

            $("#grid").data("kendoGrid").setOptions({
                editable: false
            });
            $("#grid_other").data("kendoGrid").setOptions({
                editable: false
            });
            formDivDisable();

        }else if ((viewModel.model.orderStatus=="REJECTED"||viewModel.model.orderStatus=="已拒绝")&&queryFlag == 1) {
            document.getElementById("save").style.display = "";
            document.getElementById("submit").style.display = "";
            document.getElementById("delete").style.display = "";

            document.getElementById("grid_new").style.display = "";
            document.getElementById("grid_save").style.display = "";
            document.getElementById("grid_remove").style.display = "";

            document.getElementById("approval").style.display = "none";
            document.getElementById("reject").style.display = "none";

            formDivNoDisable();
        }


        if (orderSubmitCtl=="Y"){

        }else {
            document.getElementById("submit").style.display="none";
        }
        if (orderApproveCtl=="Y"){

        }else {
            document.getElementById("approval").style.display="none";
            document.getElementById("reject").style.display="none";
        }
        /*如果状态为新建头行 初始化头默认值*/
        if (queryFlag==0){
            document.getElementById("submit").style.display="none";
            document.getElementById("delete").style.display="none";
            document.getElementById("print").style.display="none";
        }
    };


    /*如果状态为新建头行 初始化头默认值*/
    if (queryFlag==0){
        viewModel.model.orderStatus="NEW";
        viewModel.model.orderDate=new Date();
    }
   if (headerId!=null && headerId!=0) {
       var BaseUrl = _basePath;
       $.ajax({
           url: BaseUrl + "/hap/om/order/headers/queryByHeaderId",
           type:'POST', //GET
           async:true,    //或false,是否异步
           data:{
               "headerId":headerId,
           },
           dataType:'json',
           success:function(data,textStatus,jqXHR){

               var row = data.rows[0] || {};
               for (var k in row) {
                   viewModel.model.set(k, row[k]);
               };
           },
           error:function(xhr,textStatus){


           },

       })
   }

   function refreshForm(){
       if (headerId!=null && headerId!=0) {
           var BaseUrl = _basePath;
           $.ajax({
               url: BaseUrl + "/hap/om/order/headers/queryByHeaderId",
               type:'POST', //GET
               async:true,    //或false,是否异步
               data:{
                   "headerId":headerId,
               },
               dataType:'json',
               success:function(data){

                   var row = data.rows[0] || {};
                   for (var k in row) {
                       viewModel.model.set(k, row[k]);
                   };
               },
               error:function(xhr,textStatus){


               },

           })
       }
       $('#grid').data('kendoGrid').dataSource.read();
       $('#grid_other').data('kendoGrid').dataSource.read();

   };


</script>
<body>
<script src="${base.contextPath}/common/code?orderStatusData=HAP_OM_ORDER_STATUS" type="text/javascript"></script>

<div id="page-content">
    <div class="panel" id="query-form" style="padding: 0px;border:none;box-shadow: none;">
        <form class="form-horizontal" id="myForm">
            <div class="panel-body">
                <div class="row">
                    <!--销售订单号 文本框-->
                    <div class="col-xs-4">
                        <div class="form-group">
                            <label class="col-md-4 control-label"><@spring.message
                                "hscs.assess.om_order_lines.ordernumber"/></label>
                            <div class="col-xs-8">
                                <input id="orderNumber" type="text" data-role="maskedtextbox"
                                       style="float:left;width:150px;margin-right:5px;" readonly
                                       data-bind="value:model.orderNumber"
                                       class="k-textbox">
                            </div>

                        </div>
                    </div>

                    <!--公司名称 LOV-->
                    <div class="col-xs-4">
                        <div class="form-group">
                            <label class="col-md-4 control-label"><@spring.message
                                "hscs.assess.om_order_lines.companyname"/></label>
                            <div class="col-xs-8">
                                <input id="companyName" type="text" style="width:150px;margin-right:5px;" required
                                       data-bind="value:model.companyId,text:model.companyName">
                                <script>
                                    var companyName = $("#companyName").kendoLov($.extend(${lovProvider.getLov(base.contextPath, base.locale, "HAP_ORG_COMPANY_NAME")}, {
                                        select: function () {
                                            viewModel.model.set('customerId', null);
                                        },
                                        change: function () {
                                            viewModel.model.set('customerId', null);
                                        }
                                    })).data('kendoLov');
                                </script>
                            </div>
                        </div>
                    </div>
                    <!--客户名称 LOV-->
                    <div class="col-xs-4">
                        <div class="form-group">
                            <label class="col-md-4 control-label"><@spring.message
                                "hscs.assess.om_order_lines.customername"/></label>
                            <div class="col-xs-8">
                                <input id="customerName" name="customerName" type="text" style="width:150px;margin-right:5px;" required
                                       data-bind="value:model.customerId,text:model.customerName">
                                <script>

                                    var customerName =  $("#customerName").kendoLov($.extend(${lovProvider.getLov(base.contextPath, base.locale, "HAP_AR_CUSTOMER_NAME")}, {
                                        query: function (e) {
                                            //传入限制参数
                                            var a = $("#companyName").val();
                                            e.param['companyId'] = a;

                                        },
                                        select: function (e) {
                                        }
                                    })).data('kendoLov');

                                </script>
                            </div>
                        </div>
                    </div>

                </div>
                <div class="row">
                    <!--订单日期 -->
                    <div class="col-xs-4">
                        <div class="form-group">
                            <label class="col-md-4 control-label"><@spring.message
                                "hscs.assess.om_order_lines.orderdate"/></label>
                            <div class="col-xs-8">
                                <input id="orderDate" style="width:150px;margin-right:5px;" required
                                       data-bind="value:model.orderDate">
                            </div>
                            <script>
                                var datetimepicker =   $("#orderDate").kendoDatePicker({
                                    format: "{0:yyyy-MM-dd}",
                                }).data("kendoDatePicker");
                                datetimepicker.min(new Date(2016, 0, 1));
                            </script>
                        </div>
                    </div>
                    <!--订单金额 文本框-->

                    <div class="col-xs-4">
                        <div class="form-group">
                            <label class="col-md-4 control-label"><@spring.message
                                "hscs.assess.om_order_lines.ordertotalprices"/></label>
                            <div class="col-xs-8">
                                <input id="orderTotalPrices" type="text" data-role="maskedtextbox"
                                       style="float:left;width:150px;margin-right:5px;" readonly
                                       data-bind="value:model.orderTotalPrices"
                                       class="k-textbox">
                            </div>

                        </div>

                    </div>
                    <!--订单状态 下拉框-->
                    <div class="col-xs-4">
                        <div class="form-group">
                            <label class="col-md-4 control-label"><@spring.message
                                "hscs.assess.om_order_lines.orderstatus"/></label>
                            <div class="col-xs-8">
                                <input id="orderStatus" required type="text" style="width:150px;margin-right:5px;" disabled
                                       data-bind="value:model.orderStatus">
                            </div>
                            <script>
                                $("#orderStatus").kendoComboBox({
                                    dataSource: orderStatusData,
                                    valuePrimitive: true,
                                    dataTextField: "meaning",
                                    dataValueField: "value"
                                });
                            </script>
                        </div>
                    </div>
                </div>
                <div class="row" style="margin-bottom: 15px" >
                    <div class="col-xs-4">
                        <span class="btn btn-default" style="margin-right:5px;"
                              data-bind="click:saveFunction" id="save"><@spring.message "hap.save"/></span>
                        <span class="btn btn-default"  onclick="submitFunction()" id="submit"
                              style="margin-right:5px;"><@spring.message "hap.submit"/></span>
                        <span  class="btn btn-default" onclick="approvalFunction()" id="approval"
                              style="margin-right: 5px"><@spring.message "hscs.assess.om_order_lines.button.approval"/></span>
                        <span class="btn btn-default" style="margin-right:5px;" onclick="rejectFunction()" id="reject"
                              ><@spring.message "hscs.assess.om_order_lines.button.reject"/></span>
                        <span class="btn btn-default"  onclick="deleteAllSingle()" id="delete"
                              style="margin-right:5px;"><@spring.message "hscs.assess.om_order_lines.button.delete.all.single"/></span>
                        <span class="btn btn-default" onclick="printPDF()" id="print"
                              style="margin-right: 5px"><@spring.message "hscs.assess.om_order_lines.button.print.documents"/></span>
                        <span class="btn btn-default " onclick="returnFunction()"  id="return"
                              style="margin-right: 5px " data-bind="click:reset"><@spring.message "hscs.assess.om_order_lines.button.return"/></span>
                    </div>
                    <div class="col-xs-4">
                    </div>
                    <div class="col-xs-4"></div>
                </div>
                <HR align=center   SIZE=1>
                <div class="row">
                    <div class="col-xs-4">
                        <span class="btn btn-primary k-grid-add" style="float:left;margin-right:5px;" id="grid_new"
                             onclick="gridAddRow()"><@spring.message "hap.new"/></span>

                        <span class="btn btn-success k-grid-save-changes" data-bind="click:saveFunction" id="grid_save"
                              style="float:left;margin-right:5px;"><@spring.message "hap.save"/></span>

                        <span data-bind="click:remove" class="btn btn-danger" id="grid_remove"
                              style="float:left;margin-right: 5px"><@spring.message "hap.delete"/></span>
                    </div>
                    <div class="col-xs-4"></div>
                    <div class="col-xs-4">
                    </div>
                </div>
            </div>
        </form>
    </div>
    <script>kendo.bind($('#query-form'), viewModel);</script>

    <div id="tabstrip">
        <ul>
            <li id="grid1"><@spring.message "hscs.assess.om_order_lines.title.main"/></li>
            <li id="grid2"><@spring.message "hscs.assess.om_order_lines.title.other"/></li>
        </ul>
        <div style="clear:both">
            <div id="grid"></div>
        </div>
        <div style="clear:both">
            <div id="grid_other"></div>
        </div>
    </div>

    <script>
        $("#tabstrip").kendoTabStrip({
        });
        var tabToActivate = $("#grid1");
        $("#tabstrip").kendoTabStrip().data("kendoTabStrip").activateTab(tabToActivate);
    </script>




</div>

<script type="text/javascript">
    Hap.initEnterQuery('#query-form', viewModel.query);
    var BaseUrl = _basePath;
    dataSource = new kendo.data.DataSource({
        transport: {
            read: {
                url: BaseUrl + "/hap/om/order/lines/query",
                type: "POST",
                dataType: "json"
            },
            update: {
                url: BaseUrl + "/hap/om/order/lines/submit",
                type: "POST",
                contentType: "application/json"
            },
            destroy: {
                url: BaseUrl + "/hap/om/order/lines/remove",
                type: "POST",
                contentType: "application/json",
            },
            create: {
                url: BaseUrl + "/hap/om/order/lines/submit",
                type: "POST",
                contentType: "application/json"
            },
            parameterMap: function (options, type) {
                if (type !== "read" && options.models) {
                    var datas = Hap.prepareSubmitParameter(options, type);
                    if (type=="destroy"){
                        orderTotalPricesShow();
                    }
                    return kendo.stringify(datas);
                } else if (type === "read") {
                    return Hap.prepareQueryParameter(viewModel.model.toJSON(), options)
                }


            }
        },
        batch: true,
        serverPaging: false,
        pageSize: 10,
        schema: {
            data: 'rows',
            total: 'total',
            model: {
                id: "lineId",
                fields: {
                    inventoryItemId: { validation: {required: true}},
                    orderdQuantity: { type: "number" ,validation: {required: true},minValue:0},
                    unitSellingPrice: { type: "number" ,validation: {required: true}},
                    totalPrices: { type: "number" ,validation: {required: true},editable:false},

                }
            }
        }
    });
      /*************************************************************grid1**********************************************/
    $("#grid").kendoGrid({
        dataSource: dataSource,
        resizable: true,
        scrollable: true,
        navigatable: false,
        selectable: 'multiple, rowbox',
        pageable: {
            pageSizes: [5, 10, 20, 50],
            refresh: true,
            buttonCount: 5
        },

        columns: [
            {
                field: "lineNumber",
                title: '<@spring.message "hscs.assess.om_order_lines.grid.lineNumber"/>',
                width: 50,

                editor: function(container, options){
                    var html = '<span class="row-number" disabled="disabled"></span>';
                    $(html).appendTo(container);
                }
            },
            {
                field: "inventoryItemId",
                title: '<@spring.message "hscs.assess.om_order_lines.grid.itemCode"/>',
                width: 120,
                template        : function (dataItem) {
                    return dataItem['itemCode'] || '';
                },
                editor  : function (container, options) {
                    $('<input required validationMessage="<@spring.message "hss.notempty"/>" name="' + options.itemCode + '"/>')
                        .appendTo(container)
                        .kendoLov($.extend(<@lov "HAP_INV_INVENTORY_ITEMS_ITEM_CODE"/>, {
                         textField: 'itemCode',
                         model    : options.model,
                        select:function(e){
                            options.model.set('itemCode',e.item.itemCode);
                            options.model.set('itemDescription',e.item.itemDescription);
                            options.model.set('orderQuantityUom',e.item.itemUom);
                            options.model.set('inventoryItemId',e.item.inventoryItemId);
                        },
                        change : function(e){
                            if (e.sender._prev == '' || e.sender._prev == null) {
                                options.model.set('itemCode','');
                                options.model.set('itemDescription','');
                                options.model.set('orderQuantityUom','');
                                options.model.set('inventoryItemId','');
                            }
                        }
                    }));
                }
            },
            {
                field: "itemDescription",
                title: '<@spring.message "hscs.assess.om_order_lines.grid.itemDescription"/>',
                width: 120,
                editor: function(container, options){
                    var html = '<input class="k-input k-textbox k-valid" data-bind="value:itemDescription" name="itemDescription" disabled="disabled" type="text">';
                    $(html).appendTo(container);
                }
            },

            {
                field: "orderQuantityUom",
                title: '<@spring.message "hscs.assess.om_order_lines.grid.orderquantityuom"/>',
                width: 120,
                editor: function(container, options){
                    var html = '<input data-bind="value:orderQuantityUom" name="orderQuantityUom" disabled="disabled" type="text">';
                    $(html).appendTo(container);
                }
            },
            {
                field: "orderdQuantity",
                title: '<@spring.message "hscs.assess.om_order_lines.grid.orderquantity"/>',
                width: 120,
                template: function (dataItem) {
                    return dataItem['orderdQuantity'] || 0;
                },
                editor: function (container, options) {
                    $('<input id="orderdQuantity" required name="' + options.field +'" />')
                        .appendTo(container)
                        .kendoNumericTextBox({
                            min: 0,
                            format: "{0}",
                            change : function(e){
                                options.model.set('totalPrices',(options.model.unitSellingPrice * options.model.orderdQuantity).toFixed(2));
                                var grid = $("#grid").data("kendoGrid");
                                grid.refresh();
                                orderTotalPricesShow();
                            }
                        });
                }


            },

            {
                field: "unitSellingPrice",
                title: '<@spring.message "hscs.assess.om_order_lines.grid.unitsellingprice"/>',
                width: 120,
                type:"Decimal",
                template: function (dataItem) {
                    return dataItem['unitSellingPrice'] || 0;

                },
                editor: function (container, options) {
                    $('<input id="unitSellingPrice" required name="' + options.field +'" />')
                        .appendTo(container)
                        .kendoNumericTextBox({
                            min: 0,
                            format:"{0:N2}",
                            change : function(e){
                                options.model.set('totalPrices',(options.model.unitSellingPrice * options.model.orderdQuantity).toFixed(2));
                                var grid = $("#grid").data("kendoGrid");
                                grid.refresh();
                                orderTotalPricesShow();

                            }
                        });
                }

            },
             {
                field: "totalPrices",
                title: '<@spring.message "hscs.assess.om_order_headers.grid.totalPrices"/>',
                 width: 120,
                 template: function (dataItem) {
                     return (dataItem.unitSellingPrice * dataItem.orderdQuantity).toFixed(2) || 0;
                 },

            },

            {
                field: "description",
                title: '<@spring.message "hscs.assess.om_order_lines.grid.description"/>',
                width: 120,

            },

        ],
        /**** 显示行号列 ***/
        dataBound: function () {
            if (parent.autoResizeIframe) {
                parent.autoResizeIframe('${RequestParameters.functionCode!}')
            }

            var rows = this.items();
            var page = this.pager.page() - 1;
            var pagesize = this.pager.pageSize();
            $(rows).each(function () {
                var index = $(this).index() + 1 + page * pagesize;
                var rowLabel = $(this).find(".row-number");
                $(rowLabel).html(index);
            });
        },
        pageable: {
            pageSize: 2
        },
        editable: true
    });
      /***************************************************grid_other*******************************************************/
    $("#grid_other").kendoGrid({
        dataSource: dataSource,
        resizable: true,
        scrollable: true,
        navigatable: false,
        selectable: 'multiple, rowbox',
        dataBound: function () {
            if (parent.autoResizeIframe) {
                parent.autoResizeIframe('${RequestParameters.functionCode!}')
            }

            var rows = this.items();
            var page = this.pager.page() - 1;
            var pagesize = this.pager.pageSize();
            $(rows).each(function () {
                var index = $(this).index() + 1 + page * pagesize;
                var rowLabel = $(this).find(".row-number");
                $(rowLabel).html(index);
            });
        },
        pageable: {
            pageSizes: [5, 10, 20, 50],
            refresh: true,
            buttonCount: 5
        },
        columns: [
            {
                field: "lineNumber",
                title: '<@spring.message "hscs.assess.om_order_lines.grid.lineNumber"/>',
                width: 50,
                editor: function(container){
                    var html = '<span class="row-number" disabled="disabled"></span>';
                    $(html).appendTo(container);
                }
            },

            {
                field: "addition1",
                title: '<@spring.message "hscs.assess.om_order_lines.grid.addition1"/>',
                width: 120
            },
            {
                field: "addition2",
                title: '<@spring.message "hscs.assess.om_order_lines.grid.addition2"/>',
                width: 120
            },
            {
                field: "addition3",
                title: '<@spring.message "hscs.assess.om_order_lines.grid.addition3"/>',
                width: 120
            },
            {
                field: "addition4",
                title: '<@spring.message "hscs.assess.om_order_lines.grid.addition4"/>',
                width: 120
            },
            {
                field: "addition5",
                title: '<@spring.message "hscs.assess.om_order_lines.grid.addition5"/>',
                width: 120
            },
        ],
        pageable: {
            pageSize: 2
        },
        editable: true
    });
    /*添加行按钮*/
    function gridAddRow() {
        $('#grid').data('kendoGrid').addRow();
        var data=$('#grid').data('kendoGrid').dataSource.data().toJSON();
        var cd=data.length;
        var maxln=0;
        if(cd>1){
            for(var i=0;i<(cd-1);i++){
                var t=0;
                if(data[i].lineNumber>data[i+1].lineNumber)
                {
                    t=data[i+1].lineNumber;
                    data[i+1].lineNumber=data[i].lineNumber;
                    data[i].lineNumber=t;
                }
            }
            maxln=data[cd-1].lineNumber;
        }
        $('#grid').data('kendoGrid')._data[0].lineNumber=maxln*1+1;
        $("#grid").find('tr').eq(1).find('td').eq(1).text(maxln*1+1);

        $('#grid_other').data('kendoGrid')._data[0].lineNumber=maxln*1+1;
        $("#grid_other").find('tr').eq(1).find('td').eq(1).text(maxln*1+1);

    };


    //实时计算显示总金额
    function orderTotalPricesShow(delay) {

        var data = dataSource;
        var prices = 0.00;
        for (var i = 0; i < data._data.length ; i++) {
            prices=prices+(data._data[i].unitSellingPrice*data._data[i].orderdQuantity);
        }
        document.getElementById("orderTotalPrices").value=prices.toFixed(2);
    }

     //删除整个订单
    function deleteAllSingle(){
        var boo = confirm('<@spring.message "hap.tip.delete_confirm"/>')
        //confirm 会返回你选择的选项,然后可以依据选择执行逻辑
        if(boo){
            if (headerId==0||headerId==""){
                return false;
            }
            $.ajax({
                url:'/hap/om/order/lines/deleteAllSingle',
                type:'POST', //GET
                async:true,    //或false,是否异步
                data:{
                    "headerId":headerId,
                },
                // timeout:5000,    //超时时间
                dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text

                success:function(data){

                    if (data>=0){
                        Hap.showToast({
                            type: "success",
                            message: '<@spring.message "hap.successfully.delete"/>'
                        });
                    }
                    returnFunction();
                },
                error:function(){
                    Hap.showToast({
                        type: "error",
                        message: '<@spring.message "hap.fail.delete"/>'
                    });
                },
            })

        }else{

        }


    }

    //提交按钮
     function submitFunction() {
        viewModel.model.orderStatus="已提交";
        $("#save").click();
        refreshForm();
         buttonControl();
    };
    //审批按钮
    function approvalFunction(){
        viewModel.model.orderStatus="已审批";
        $("#save").click();
        refreshForm();
        buttonControl();
    };
    //拒绝按钮
    function rejectFunction() {
        viewModel.model.orderStatus="已拒绝";
        $("#save").click();
        refreshForm();
        buttonControl();
    };




    //返回按钮
    function returnFunction() {
        //刷新父页面
        window.self.parent.location.reload();
    };


    /*根据订单状态控制按钮显示*/
    function buttonControl() {

        if ((viewModel.model.orderStatus=="NEW"||viewModel.model.orderStatus=="新建")&&queryFlag == 1) {
            document.getElementById("delete").style.display="";
            document.getElementById("save").style.display="";
            document.getElementById("submit").style.display="";

            document.getElementById("grid_new").style.display="";
            document.getElementById("grid_save").style.display="";
            document.getElementById("grid_remove").style.display="";

            document.getElementById("approval").style.display="none";
            document.getElementById("reject").style.display="none";
            formDivNoDisable();

        }else if ((viewModel.model.orderStatus=="SUBMITED"||viewModel.model.orderStatus=="已提交")&&queryFlag == 1) {
            document.getElementById("approval").style.display="";
            document.getElementById("reject").style.display="";

            document.getElementById("delete").style.display="none";
            document.getElementById("save").style.display="none";
            document.getElementById("submit").style.display="none";

            document.getElementById("grid_new").style.display="none";
            document.getElementById("grid_save").style.display="none";
            document.getElementById("grid_remove").style.display="none";

            $("#grid").data("kendoGrid").setOptions({
                editable: false
            });
            $("#grid_other").data("kendoGrid").setOptions({
                editable: false
            });
            formDivDisable();
        }else if( (viewModel.model.orderStatus=="APPROVED"||viewModel.model.orderStatus=="已审批")&&queryFlag == 1){

            document.getElementById("save").style.display="none";
            document.getElementById("submit").style.display="none";
            document.getElementById("approval").style.display="none";
            document.getElementById("reject").style.display="none";
            document.getElementById("delete").style.display="none";

            document.getElementById("grid_new").style.display="none";
            document.getElementById("grid_save").style.display="none";
            document.getElementById("grid_remove").style.display="none";

            $("#grid").data("kendoGrid").setOptions({
                editable: false
            });
            $("#grid_other").data("kendoGrid").setOptions({
                editable: false
            });
            formDivDisable();
        }else if ((viewModel.model.orderStatus=="REJECTED"||viewModel.model.orderStatus=="已拒绝")&&queryFlag == 1){
            document.getElementById("save").style.display="";
            document.getElementById("submit").style.display="";
            document.getElementById("delete").style.display="";

            document.getElementById("grid_new").style.display="";
            document.getElementById("grid_save").style.display="";
            document.getElementById("grid_remove").style.display="";

            document.getElementById("approval").style.display="none";
            document.getElementById("reject").style.display="none";

            formDivNoDisable();
        }

        if (orderSubmitCtl=="Y"){

        }else {
            document.getElementById("submit").style.display="none";
        }
        if (orderApproveCtl=="Y"){

        }else {
            document.getElementById("approval").style.display="none";
            document.getElementById("reject").style.display="none";
        }
    }

function formDivDisable() {
    document.getElementById("orderNumber").disabled="disabled";
    companyName.enable(false);
    customerName.enable(false);
    datetimepicker.enable(false);
};

    function formDivNoDisable() {
        document.getElementById("orderNumber").disabled="";
        companyName.enable(true);
        customerName.enable(true);
        datetimepicker.enable(true);
    }



</script>
<script src="https://cdn.bootcss.com/html2canvas/0.5.0-beta4/html2canvas.js"></script>
<script src="https://cdn.bootcss.com/jspdf/1.4.0/jspdf.debug.js"></script>
<script>

    function printPDF() {
        html2canvas(
            document.getElementById("page-content"), {
                dpi:172,//导出pdf的清晰度
                onrendered: function(canvas) {

                    var contentWidth = canvas.width;
                    var contentHeight = canvas.height;

                    //一页pdf显示html页面生成的canvas高度;
                    var pageHeight = contentWidth / 592.28 * 841.89;

                    //未生成pdf的html页面高度
                    var leftHeight = contentHeight;

                    //pdf页面偏移
                    var position = 0;

                    //html页面生成的canvas在pdf中图片的宽高（a4纸的尺寸[595.28,841.89]）
                    var imgWidth = 595.28;
                    var imgHeight = 592.28 / contentWidth * contentHeight;

                    var pageData = canvas.toDataURL('image/jpeg', 1.0);
                    var pdf = new jsPDF('', 'pt', 'a4');


                    //有两个高度需要区分，一个是html页面的实际高度，和生成pdf的页面高度(841.89)
                    //当内容未超过pdf一页显示的范围，无需分页
                    if (leftHeight < pageHeight) {
                        pdf.addImage(pageData, 'JPEG', 0, 0, imgWidth, imgHeight);
                    } else {
                        while (leftHeight > 0) {
                            pdf.addImage(pageData, 'JPEG', 0, position, imgWidth, imgHeight)
                            leftHeight -= pageHeight;
                            position -= 841.89;
                            //避免添加空白页
                            if (leftHeight > 0) {
                                pdf.addPage();
                            }
                        }
                    }
                    pdf.save('PDF打印.pdf');
                },
                //背景设为白色（默认为黑色）
                background: "#fff"
            });
    }
</script>



</body>
</html>